package cn.itcast.controller;

import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.Result;

import cn.itcast.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping(path = "/report")
@CrossOrigin
public class ReportController {

    @Reference
    private ReportMemberService reportMemberService;

    @Reference
    private ReportSetmealService reportSetmealService;

    @Reference
    private ReportBusinessService reportBusinessService;

    @Reference
    private ReportOrderService reportOrderService;

    @Reference
    ReportIncomeService reportIncomeService;

    @Autowired
    public IUserService iUserService;

    /**
     * 每月会员数量统计
     * @return
     */
    @RequestMapping(path = "/member/count")
    public Result reportMemberByCount(){
        HashMap<String, Object> map = new HashMap<>(16);

        List<Map<String, Object>> list = reportMemberService.reportMemberByCount();
        List<Object> months = new ArrayList<>();
        List<Object> memberCount = new ArrayList<>();

        for (Map<String, Object> objectMap : list) {
            months.add( objectMap.get("months"));
            memberCount.add(objectMap.get("memberCount"));
        }

        map.put("months",months);
        map.put("memberCount",memberCount);

        return new Result(true, MessageConstant.UPLOAD_SUCCESS, map);

    }

    @RequestMapping(path = "/setmeals")
    public Result reportByCount(){

        Map<String, Object> map = reportSetmealService.reportSetmealByCount();

        return new Result(true, MessageConstant.UPLOAD_SUCCESS, map);

    }

    @RequestMapping(path = "/business")
    public Result reportBusiness(){
        HashMap<String, Object> map = new HashMap<>(16);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();

        map.put("reportDate",simpleDateFormat.format(date));
        map.put("todayNewMember",reportBusinessService.reportTodayNewMember().get("todayNewMember"));
        map.put("totalMember",reportBusinessService.reportTotalMember().get("totalMember"));
        map.put("thisWeekNewMember",reportBusinessService.reportThisWeekNewMember().get("thisWeekNewMember"));
        map.put("thisMonthNewMember",reportBusinessService.reportThisMonthNewMember().get("thisMonthNewMember"));
        map.put("todayOrderNumber",reportBusinessService.reportTodayOrderNumber().get("todayOrderNumber"));
        map.put("todayVisitsNumber",reportBusinessService.reportTodayVisitsNumber().get("todayVisitsNumber"));
        map.put("thisWeekOrderNumber",reportBusinessService.reportThisWeekOrderNumber().get("thisWeekOrderNumber"));
        map.put("thisWeekVisitsNumber",reportBusinessService.reportThisWeekVisitsNumber().get("thisWeekVisitsNumber"));
        map.put("thisMonthOrderNumber",reportBusinessService.reportThisMonthOrderNumber().get("thisMonthOrderNumber"));
        map.put("thisMonthVisitsNumber",reportBusinessService.reportThisMonthVisitsNumber().get("thisMonthVisitsNumber"));

        List<Map<String, Object>> list = reportBusinessService.reportHotSetmeal();

        map.put("hotSetmeal",list);

        return new Result(true, MessageConstant.UPLOAD_SUCCESS, map);
    }

    @GetMapping(path = "/export/business")
    public void reportExportBusiness(HttpServletResponse response) throws IOException {
        //调接口
        iUserService.excelExport(response);
    }


    @RequestMapping(path = "/order/count")
    public Result reportOrder(){
        List<Map<String, Object>> mapList = reportOrderService.reportOrderCounts();
        List<Object> orderMonths = new ArrayList<>();
        List<Object> orderCounts = new ArrayList<>();
        List<Object> payCounts = new ArrayList<>();

        Map<String, Object> map = new HashMap<>(16);

        for (Map<String, Object> maps : mapList) {
            orderMonths.add(maps.get("orderMonths"));
            orderCounts.add(maps.get("orderCounts"));
            payCounts.add(maps.get("payCounts"));
        }
        map.put("orderMonths",orderMonths);
        map.put("orderCounts",orderCounts);
        map.put("payCounts",payCounts);

        return new Result(true, MessageConstant.UPLOAD_SUCCESS, map);
    }

    @RequestMapping(path = "/income/money")
    public Result reportPayMoney(){
        List<Map<String, Object>> mapList = reportIncomeService.reportPayMoney();

        HashMap<String, Object> maps = new HashMap<>(16);

        Map<String, Object> map = new HashMap<>(16);

        List<Object> orderMonths = new ArrayList<>();

        List<Object> payMoney = new ArrayList<>();

        for (Map<String, Object> objectMap : mapList) {
            orderMonths.add(objectMap.get("orderMonths"));
            payMoney.add(objectMap.get("payMoney"));
        }

        map.put("orderMonths",orderMonths);
        map.put("payMoney",payMoney);

        return new Result(true, MessageConstant.UPLOAD_SUCCESS, map);
    }




}
